package com.tsp.ssm.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by 隼 on 2016/12/5.
 * 登陆认证拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //获取请求的url

//        String url = httpServletRequest.getRequestURI();
//        //判断url是否是公开 地址（实际使用时将公开 地址配置配置文件中）
//        //这里公开地址是登陆提交的地址
//
//        if(url.indexOf("login.action")>=0){
//            //如果进行登陆提交，放行
//            return true;
//        }
//
//        //判断session
//        HttpSession session  = httpServletRequest.getSession();
//        //从session中取出用户身份信息
//        String username = (String) session.getAttribute("username");
//
//        if(username != null&&username!=""){
//            //身份存在，放行
//            return true;
//        }
//
//        //执行这里表示用户身份需要认证，跳转登陆页面
//        httpServletRequest.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(httpServletRequest, httpServletResponse);
//
//        //return false表示拦截，不向下执行
//        //return true表示放行
//        return false;
        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
